import {configDefaults, defineConfig } from "vitest/config";
import { readdirSync, statSync } from 'node:fs'
import path from 'node:path'
import { fileURLToPath } from 'node:url'
const resolveEntryForPkg = (/** @type {string} */ p) =>
  path.resolve(
    fileURLToPath(import.meta.url),
    `./packages/${p}/src/index.ts`,
  )
const dirs = readdirSync(new URL('./packages', import.meta.url))
/** @type {Record<string, string>} */
const entries = {
  miew: resolveEntryForPkg('miew'),
}
const nonSrcPackages = ['playground']

for (const dir of dirs) {
  const key = `@miew/${dir}`
  if (
    dir !== 'miew' &&
    !nonSrcPackages.includes(dir) &&
    !(key in entries) &&
    statSync(new URL(`./packages/${dir}`, import.meta.url)).isDirectory()
  ) {
    entries[key] = resolveEntryForPkg(dir)
  }
}
export default defineConfig({
  define: {
    __DEV__: true,
    __TEST__: true,
    __VERSION__: '"test"',
    __BROWSER__: false,
    __GLOBAL__: false,
    __ESM_BUNDLER__: true,
    __ESM_BROWSER__: false,
    __CJS__: true,
    __SSR__: true,
    __FEATURE_OPTIONS_API__: true,
    __FEATURE_SUSPENSE__: true,
    __FEATURE_PROD_DEVTOOLS__: false,
    __FEATURE_PROD_HYDRATION_MISMATCH_DETAILS__: false,
    __COMPAT__: true,
  },
  test: {
    globals: true,
    setupFiles: 'scripts/setup-vitest.ts',
    environmentMatchGlobs: [
      ['packages/{vue,vue-compat,runtime-dom}/**', 'jsdom'],
    ],
    sequence: {
      hooks: 'list',
    },
    coverage: {
      provider: 'istanbul',
      reporter: ['text', 'html'],
      exclude: [
        ...configDefaults.coverage.exclude!,
         // DOM transitions are tested via e2e so no coverage is collected
         'packages/runtime-dom/src/components/Transition*',
         // mostly entries
        //  'packages/vue-compat/**',
      ]
    }
  },
  resolve: {
    alias: entries
    // [
    //   {
    //     find: /@miew\/([\w-]*)/,
    //     replacement: path.resolve(__dirname, "packages") + "/$1/src",
    //   },
    // ],
  },

});
